<template>
  <div class="app-container" style="min-height: auto">
    <!-- 表格-条件筛区域  -->
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus"
                 @click="handleCreate"> 新增
      </el-button>
        <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus"
                   @click="batchDialogVisible = true "> 批量场次
        </el-button>
    </div>

    <!-- 表格-内容区域 -->
    <el-table v-loading="listLoading" :key="tableKey" :data="tableList" border fit highlight-current-row
              style="width: 100%;">
      <el-table-column v-for="column in columns" :key="column.prop"
                       :prop="column.prop"
                       :label="column.label"
                       :width="column.width"
                       :align="column.align?column.align:'center'"
                       :min-width="column.minWidth"
                       :sortable="column.sortable"
                       :fixed="column.fixed"
                       :type="column.type" :formatter="column.formatter">
        <template slot-scope="scope">
          <!-- 行内显示图片 -->
          <img v-if="column.imgFlag==true"
               :src=" scope.row[column.prop].indexOf('http://') >-1 ?  scope.row[column.prop] : ( CMBASE.baseFileContext + scope.row[column.prop] ) "
               width="60" height="60" class="img"/>
          <!-- 行内显示开关按钮 -->
          <el-switch v-else-if="column.switchFlag==true" v-model="scope.row[column.prop]" :active-value="1"
                     :inactive-value="0" disabled/>
          <!-- 行内显示tag标签 -->
          <span v-else-if="column.tagFlag==true">
                        <span v-for="item in column.tagFormatter" :key="item.value">
                            <el-tag v-if="item.value==scope.row[column.prop]" :type="item.type">{{ item.text }}</el-tag>
                        </span>
                    </span>
          <!-- 行内格式自定义 -->
          <span v-else-if="column.formatter"> {{ column.formatter(scope.row) }}</span>
          <!--默认显示文本-->
          <span v-else>{{ scope.row[column.prop] }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="240" class-name="small-padding">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" class="el-button--primary" @click="handleUpdate(scope.row)"> 编辑
          </el-button>
          <el-button type="text" icon="el-icon-delete" class="el-button--danger" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 表格-页脚区域 -->
    <pagination v-show="tableTotal>0" :total="tableTotal" :page.sync="pageRequest.pageNum"
                :limit.sync="pageRequest.pageSize" @pagination="getList"/>

    <!--新增编辑界面-->
    <el-dialog :modal="false" :title="operation?'新增':'编辑'" width="45%" top="5vh"  :visible.sync="dialogFormVisible"
               :close-on-click-modal="false">
      <el-form :model="dataForm" label-width="140px" :rules="dataFormRules" ref="dataForm" style="width: 100%">

        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker type="datetime" placeholder="选择日期" v-model="dataForm.startTime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          style="width: 100%;"></el-date-picker>
        </el-form-item>
        <el-form-item label="预约人数限制" prop="signCountLimit">
          <el-input-number v-model="dataForm.signCountLimit" :min="1" :max="1000"
                           label="预约人数限制"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click.native="closeDialog()">取消</el-button>
        <el-button size="mini" @click.native="saveFormData()" :loading="editLoading" type="primary">提交</el-button>
      </div>
    </el-dialog>


      <el-dialog
        title="批量新增考试场次"
        :visible.sync="batchDialogVisible"
        top="5vh"
        :close-on-click-modal="false"
        width="45%"
        :modal="false"
      >
          <el-form :model="checkFrom" label-width="140px" :rules="dataFormRules" ref="checkFrom" style="width: 100%">
              <el-form-item label="考试周">
                  <el-checkbox-group v-model="checkFrom.checkList">
                      <el-checkbox label="MONDAY">{{ "周一" }}</el-checkbox>
                      <el-checkbox label="TUESDAY">{{ "周二" }}</el-checkbox>
                      <el-checkbox label="WEDNESDAY">{{ "周三" }}</el-checkbox>
                      <el-checkbox label="THURSDAY">{{ "周四" }}</el-checkbox>
                      <el-checkbox label="FRIDAY">{{ "周五" }}</el-checkbox>
                      <el-checkbox label="SATURDAY">{{ "周六" }}</el-checkbox>
                      <el-checkbox label="SUNDAY">{{ "周日" }}</el-checkbox>
                  </el-checkbox-group>
              </el-form-item>

              <el-form-item label="考试时间">
                  <el-col :span="11">
                      <el-time-picker  placeholder="选择考试开始时间" value-format="HH:mm:ss" v-model="checkFrom.date1" style="width: 100%;"></el-time-picker>
                  </el-col>
                  <el-col class="line" :span="2">-</el-col>
<!--                  <el-col :span="11">-->
<!--                      <el-time-picker  placeholder="选择考试结束时间" value-format="HH:mm:ss" v-model="checkFrom.date2" style="width: 100%;"></el-time-picker>-->
<!--                  </el-col>-->
              </el-form-item>
              <el-form-item label="批量考试开始日期" >
                  <el-date-picker  placeholder="选择开始日" value-format="yyyy-MM-dd" v-model="checkFrom.date3"  style="width: 100%;"></el-date-picker>
              </el-form-item>
              <el-form-item label="批量考试结束日期">
                  <el-date-picker  placeholder="选择结束日" value-format="yyyy-MM-dd" v-model="checkFrom.date4" style="width: 100%;"></el-date-picker>
              </el-form-item>
              <el-form-item label="每场预约人数限制">
                  <el-input-number v-model="checkFrom.signCountLimit" :min="1" :max="1000"
                                   label="预约人数限制"></el-input-number>
              </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
              <el-button size="mini" @click.native="closeChickDialog()">取消</el-button>
              <el-button size="mini" @click.native="saveFormCheck()" :loading="editLoading" type="primary">提交</el-button>
          </div>
      </el-dialog>


  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import {checkExamSave} from "@/api/modules/examApi"; // 分页栏信息
export default {
  name: 'examLot',
  components: {
    Pagination,
  },
  directives: {},
  filters: {},
  props: {
    id: {
      type: String,
      default: ''
    },
  },
  data() {
    return {
      tableKey: 0,
      tableList: [],//表格数据源
      tableTotal: 0,//表格总条数数据
      listLoading: true,
      batchDialogVisible: false,

      pageRequest: {
        pageNum: 1, pageSize: 10,
        searchParam: {},
        orderParam:{ start_time:false }
      },
      searchParam: {},
      dialogFormVisible: false,//默认不显示弹出框
      operation: true, // true:新增,w false:编辑
      editLoading: false,//保存表单时loading框
      columns: [
        {prop: 'createTime', label: '创建时间', sortable: false},
        {prop: 'startTime', label: '开始时间', sortable: false},
        {prop: 'signCountLimit', label: '预约人数限制', sortable: false},
      ],
      fileList: [],
      // 新增编辑界面数据
      dataForm: {},
      checkFrom: {
          checkList: [],
          signCountLimit:10
      },
      //表单检验规则
      dataFormRules: {
        startTime: [{required: true, message: '请输入开始时间', trigger: 'blur'}],
        signCountLimit: [{required: true, message: '请输入预约人数限制', trigger: 'blur'}],
      },
    }
  },
  created() {
    this.getList();
  },
  methods: {
    //获取表格数据
    getList() {
      this.listLoading = true
      //【API请求】----获取表格列表数据
      this.pageRequest.searchParam.SEARCH_EQ_exam_id = this.id
      this.$api.getSExamLotList(this.pageRequest).then(response => {
        this.tableList = response.data.content
        this.tableTotal = response.data.total
        this.listLoading = false;
      }).catch((res) => {
        this.$message({message: '列表获取失败', type: 'error'})
      })
    },
    //表格数据筛选
    handleFilter() {
        this.pageRequest = {
            pageNum: 1,
            pageSize: 10,
        }
      this.pageRequest.searchParam = this.searchParam
      this.getList()
    },
    //【按钮点击】--- 弹出新增表单
    handleCreate() {
      this.operation = true;
      this.initDataForm();
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
        this.editLoading = false;
      })
    },
    //【按钮点击】--- 弹出编辑表单
    handleUpdate(row) {
      this.operation = false;
      this.dataForm = Object.assign({}, row)
      //显示编辑菜单
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
        this.editLoading = false;
      })
    },
    //保存表单数据
    saveFormData() {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false;
        }
        this.editLoading = true;
        //【API请求】----保存数据
        this.dataForm.examId = this.id
        this.$api.saveSExamLot(this.dataForm).then(() => {
          this.editLoading = false;//关闭loading框
          this.dialogFormVisible = false;//关闭表单页面
          this.initDataForm();//重置表单数据
          this.getList();//重新获取表格数据
          this.$notify({title: '成功', message: '保存成功', type: 'success', duration: 2000})
        })
      })
    },
    saveFormCheck(){
        this.editLoading=true
      this.$refs['checkFrom'].validate((valid) => {
          console.log(valid , 'log')
          if (!valid){
              return false;
          }
          if (this.checkFrom.checkList){
              this.checkFrom.checkString = this.checkFrom.checkList.join(",")
          }
          this.checkFrom.examId = this.id
          console.log(this.checkFrom , 'log')
          this.$api.checkExamSave(this.checkFrom).then(res=>{
                this.editLoading = false
                this.batchDialogVisible = false
                this.initDataForm();
                this.getList();
          });
      })



    },
    //【按钮点击】---删除行
    handleDelete(row) {
      this.$confirm('确认删除该记录吗？', '提示', {}).then(() => {
        //【API请求】---- 删除数据
        this.$api.deleteSExamLot({ids: row.id}).then(() => {
          this.$message({message: '操作成功', type: 'success'})
          this.pageRequest.pageNum = 1
          this.getList();
        })
      }).catch(() => {
        this.$message({message: '操作失败', type: 'error'})
      });
    },
    //初始化表单数据，重新渲染
    initDataForm() {
      this.dataForm = {
        startTime: '',
        examId: '',
        signCountLimit: '',
        id: '',
      };
      this.checkFrom = {
          checkList:[],
          data1:'',
          data2:'',
          data3:'',
          data4:'',
          signCountLimit:'',
      };
    },
    //关闭表单弹出框
    closeDialog() {
      this.$refs['dataForm'].resetFields();
      this.dialogFormVisible = false
    },
    closeChickDialog(){
        this.$refs['checkFrom'].resetFields();
        this.batchDialogVisible = false;
    }

  }


}
</script>
